﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using IF.Utils;

namespace IFStudio
{
    public partial class Form_AnalysisOrderDetal : Form
    {
        public Form_AnalysisOrderDetal()
        {
            InitializeComponent();
        }

        #region _____变量_____

        //文件路径
        string GLOBAL_PATH = string.Empty;
        string GLOBAL_SAVEFILEPATH = string.Empty;
        string global_col_arry = string.Empty;
        string GLOBAL_SHOP_NAME = string.Empty;
        #endregion

        //获取文件路径
        private void Form_AnalysisOrderDetal_DragEnter(object sender, DragEventArgs e)
        {
            this.GLOBAL_PATH = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            this.txt_Path.Text = this.GLOBAL_PATH;
        }

        private void bgw_Order_DoWork(object sender, DoWorkEventArgs e)
        {
            switch (e.Argument.ToString())
            {
                case "btn_Analysis":
                    ProcesOrder_JDZhongChou();
                    //ProcesOrder_DieYingChongChong();
                    //ProcesOrder_AiChong();
                    break;
                case "btn_ProductPick":
                    ProcessProductPick();
                    break;
                case "btn_UpdateWuLiu":
                    UpdateWuLiuNumber();
                    break;
                case "btn_UpdateFaHuoDan":
                    UpdateFaHuoDan();
                    break;
                default:
                    break;
            }
            e.Result = "\r\n----------本次操作结束----------";
        }

        private void bgw_Order_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.txt_ShowInfo.AppendText(e.UserState.ToString());
        }

        private void bgw_Order_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.txt_ShowInfo.AppendText(e.Result.ToString() + "\r\n");
        }

        private void btn_Analysis_Click(object sender, EventArgs e)
        {
            //判断路径是否存在
            if (string.IsNullOrEmpty(this.GLOBAL_PATH))
            {
                this.bgw_Order.ReportProgress(1, "\r\n**************请将文件拖入应用**************\r\n");
                return;
            }
            //判断文件是否存在
            if (!File.Exists(this.GLOBAL_PATH))
            {
                this.bgw_Order.ReportProgress(1, "\r\n**************文件不存在**************\r\n");
                return;
            }
            this.global_col_arry = this.txt_col_array.Text;
            this.GLOBAL_SHOP_NAME = this.txt_shop_name.Text.Trim();
            this.bgw_Order.RunWorkerAsync("btn_Analysis");
        }

        private void btn_ProductPick_Click(object sender, EventArgs e)
        {
            //判断路径是否存在
            if (string.IsNullOrEmpty(this.GLOBAL_PATH))
            {
                this.bgw_Order.ReportProgress(1, "\r\n**************请将文件拖入应用**************\r\n");
                return;
            }
            //判断文件是否存在
            if (!File.Exists(this.GLOBAL_PATH))
            {
                this.bgw_Order.ReportProgress(1, "\r\n**************文件不存在**************\r\n");
                return;
            }
            this.bgw_Order.RunWorkerAsync("btn_ProductPick");
        }

        //备货处理
        private void ProcessProductPick()
        {
            this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>读取，");
            DataTable DT = NOPIHelper.RenderDataTableFromExcel(this.GLOBAL_PATH, "Table1", 0);
            this.bgw_Order.ReportProgress(1, "完成<<<<<<<<<<<<\r\n");
            //判断文件是否有数据
            if (DT.Columns.Count == 0)
            {
                this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>excel无数据\r\n");
                return;
            }

            //创建数据表
            DataTable tblDatas = new DataTable();
            DataColumn dc = null;
            //单据类型
            dc = tblDatas.Columns.Add("单据类型", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("期初标志", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("发货单号", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("发货日期", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("业务类型", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("销售类型编号", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("客户编码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("部门编码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("币种", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("汇率", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("表头税率", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("退货标志", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("先发货先开票标志", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("仓库编码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("存货编码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("主计量单位编码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("主计量数量", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("报价含税", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("表头自定义项4", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("表头自定义项11", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("制单人", Type.GetType("System.String"));

            DataRow newRow;

            int currentinmdex = 0;
            foreach (DataRow item in DT.Rows)
            {
                //发货批次
                string lcal_FHPI = item["发货批次"].ToString();
                for (int i = 0; i < Convert.ToInt32(item["发货单量"].ToString()); i++)
                {
                    currentinmdex++;
                    //5个品类

                    for (int t = 7; t < DT.Columns.Count; t++)
                    {
                        //如果数量为0就跳过
                        if (string.IsNullOrEmpty(item[DT.Columns[t].ColumnName].ToString()))
                        {
                            continue;
                        }
                        newRow = tblDatas.NewRow();
                        newRow["单据类型"] = "05";
                        newRow["表头自定义项4"] = item["发货日期"].ToString();
                        newRow["表头自定义项11"] = lcal_FHPI;
                        newRow["期初标志"] = "0";
                        newRow["发货单号"] = "DRFH" + DateTime.Now.ToString("yyyyMMdd") + currentinmdex.ToString().PadLeft(4, '0'); ;
                        newRow["发货日期"] = DateTime.Now.ToShortDateString();
                        newRow["业务类型"] = "普通销售";
                        newRow["销售类型编号"] = "01";
                        newRow["客户编码"] = "01002";
                        newRow["部门编码"] = "0201";//项目部
                        newRow["币种"] = "人民币";
                        newRow["汇率"] = "17";
                        newRow["表头税率"] = "17";
                        newRow["退货标志"] = "0";
                        newRow["先发货先开票标志"] = "0";
                        newRow["仓库编码"] = "01";
                        newRow["存货编码"] = DT.Columns[t].ColumnName;
                        newRow["主计量单位编码"] = "PCS";
                        newRow["主计量数量"] = item[DT.Columns[t].ColumnName].ToString();
                        newRow["报价含税"] = "0";
                        newRow["制单人"] = "范莲莲";
                        tblDatas.Rows.Add(newRow);
                    }
                }
                this.bgw_Order.ReportProgress(1, item[8].ToString() + "\r\n");
            }
            string[] arrayA = { "单据类型", "期初标志", "发货单号", "发货日期", "业务类型", "销售类型编号", "客户编码", "部门编码", "币种", "汇率", "表头税率",
                "退货标志", "先发货先开票标志", "仓库编码", "存货编码", "主计量单位编码", "主计量数量", "报价含税", "表头自定义项4","表头自定义项11", "制单人" };
            string[] arrayB = arrayA;
            NOPIHelper.Export(tblDatas, "", "E:\\其他\\123.xls", "sheet1", arrayA, arrayB);
        }

        private void ProcesOrder_AiChong()
        {
            this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>读取，");
            DataTable DT = NOPIHelper.RenderDataTableFromExcel(this.GLOBAL_PATH, "Table1", 0);
            this.bgw_Order.ReportProgress(1, "完成<<<<<<<<<<<<\r\n");
            //判断文件是否有数据
            if (DT.Columns.Count == 0)
            {
                this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>excel无数据\r\n");
                return;
            }
            this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>加载价格\r\n");
            Dictionary<string, double> price = new Dictionary<string, double>();
            price.Add("ACBJB0101", 56);
            price.Add("ACBJB0103", 37);
            price.Add("ACBQBTZ0101", 1);
            price.Add("ACCPQT01", 33);
            price.Add("ACFBB0101", 14.35);
            price.Add("ACHLBYCB0101", 27.5);
            price.Add("ACPPT0101", 2.3);
            price.Add("ACTX0101", 46);
            price.Add("ACTZ0101", 1.1);
            price.Add("ACXZH0101", 49);
            price.Add("ACCPYZ01", 0);

            //创建数据表
            DataTable tblDatas = new DataTable();
            DataColumn dc = null;
            //单据类型
            dc = tblDatas.Columns.Add("单据类型", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("期初标志", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("发货单号", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("发货日期", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("业务类型", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("销售类型编号", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("客户编码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("部门编码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("币种", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("汇率", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("表头税率", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("退货标志", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("先发货先开票标志", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("仓库编码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("存货编码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("主计量单位编码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("主计量数量", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("报价含税", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("表头自定义项4", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("表头自定义项11", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("表头自定义项12", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("表头自定义项13", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("表头自定义项14", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("制单人", Type.GetType("System.String"));

            dc = tblDatas.Columns.Add("表头备注", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("价税合计_原币", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("税额_原币", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("金额_原币_无税", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("无税单价", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("含税单价_原币", Type.GetType("System.String"));
            DataRow newRow;

            int currentinmdex = 0;
            foreach (DataRow item in DT.Rows)
            {
                this.bgw_Order.ReportProgress(1, "--------------------------------------------------------\r\n");
                currentinmdex++;
                //5个品类

                for (int t = 8; t < DT.Columns.Count; t++)
                {
                    string temp_ProductCount = item[DT.Columns[t].ColumnName].ToString().Trim();
                    if (string.IsNullOrEmpty(temp_ProductCount) || Convert.ToInt32(temp_ProductCount) == 0)
                    {
                        //过滤
                        continue;
                    }
                    newRow = tblDatas.NewRow();
                    newRow["单据类型"] = "05";
                    newRow["期初标志"] = "0";
                    newRow["发货单号"] = "DRFH" + DateTime.Now.ToString("yyyyMMddHHmm") + currentinmdex.ToString().PadLeft(4, '0');
                    newRow["发货日期"] = DateTime.Now.ToShortDateString();
                    newRow["业务类型"] = "普通销售";
                    newRow["销售类型编号"] = "01";//普通销售
                    newRow["客户编码"] = "07016";//环球中国
                    newRow["部门编码"] = "0201";//项目部
                    newRow["币种"] = "人民币";
                    newRow["汇率"] = "1";
                    newRow["表头税率"] = "17";
                    newRow["退货标志"] = "0";
                    newRow["先发货先开票标志"] = "0";
                    newRow["仓库编码"] = "01";
                    newRow["存货编码"] = DT.Columns[t].ColumnName;
                    newRow["主计量单位编码"] = "PCS";
                    newRow["主计量数量"] = temp_ProductCount;
                    newRow["报价含税"] = 1;
                    newRow["表头自定义项4"] = item["发货日期"].ToString();
                    newRow["表头自定义项11"] = item["批次单号"].ToString();
                    newRow["表头自定义项12"] = item["收货人姓名"].ToString();
                    newRow["表头自定义项13"] = item["地址"].ToString();
                    newRow["表头自定义项14"] = item["收货人电话"].ToString();
                    newRow["制单人"] = "范莲莲";

                    //含税单价_原币	无税单价	金额_原币_无税	税额_原币	价税合计_原币
                    //117 100 100 17  117

                    newRow["表头备注"] = "";
                    //newRow["含税单价_原币"] = price[DT.Columns[t].ColumnName] * (1.17);
                    //newRow["价税合计_原币"] = price[DT.Columns[t].ColumnName] * (1.17) * Convert.ToInt32(temp_ProductCount);
                    //newRow["税额_原币"] = price[DT.Columns[t].ColumnName] * 0.17;
                    //newRow["金额_原币_无税"] = price[DT.Columns[t].ColumnName] * Convert.ToInt32(temp_ProductCount);
                    //newRow["无税单价"] = price[DT.Columns[t].ColumnName];

                    double wushuidanjia = Math.Round(price[DT.Columns[t].ColumnName] / 1.17, 2, MidpointRounding.AwayFromZero);
                    newRow["含税单价_原币"] = price[DT.Columns[t].ColumnName];
                    newRow["价税合计_原币"] = price[DT.Columns[t].ColumnName] * Convert.ToInt32(temp_ProductCount);
                    newRow["税额_原币"] = price[DT.Columns[t].ColumnName] - wushuidanjia;
                    newRow["金额_原币_无税"] = wushuidanjia * Convert.ToInt32(temp_ProductCount);
                    newRow["无税单价"] = wushuidanjia;





                    tblDatas.Rows.Add(newRow);
                    this.bgw_Order.ReportProgress(1, DT.Columns[t].ColumnName + "\t" + item["收货人电话"].ToString() + "\t" + item["收货人姓名"].ToString() + "\t" + item["地址"].ToString() + "\r\n");
                }

                //this.bgw_Order.ReportProgress(1, "--------------------------------------------------------\r\n");
            }
            string[] arrayA = { "单据类型", "期初标志", "发货单号", "发货日期", "业务类型", "销售类型编号", "客户编码", "部门编码", "币种", "汇率", "表头税率",
                "退货标志", "先发货先开票标志", "仓库编码", "存货编码", "主计量单位编码", "主计量数量", "报价含税","无税单价" ,"金额_原币_无税","税额_原币","含税单价_原币","价税合计_原币",
                "表头备注", "表头自定义项4","表头自定义项11", "制单人", "表头自定义项12", "表头自定义项13", "表头自定义项14"};
            string[] arrayB = arrayA;
            NOPIHelper.Export(tblDatas, "", "E:\\其他\\123.xls", "sheet1", arrayA, arrayB);
        }
        private void ProcesOrder_DieYingChongChong()
        {
            this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>读取，");
            DataTable DT = NOPIHelper.RenderDataTableFromExcel(this.GLOBAL_PATH, "Table1", 0);
            this.bgw_Order.ReportProgress(1, "完成<<<<<<<<<<<<\r\n");
            //判断文件是否有数据
            if (DT.Columns.Count == 0)
            {
                this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>excel无数据\r\n");
                return;
            }
            this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>加载价格\r\n");
            Dictionary<string, double> price = new Dictionary<string, double>();
            price.Add("DYCP01", 19);
            price.Add("DYCP02", 13.8);
            price.Add("DYCP03", 4.6);
            price.Add("DYCP04", 4.6);
            price.Add("DYCP05", 4.6);
            price.Add("DYCP06", 4.8);
            price.Add("DYCP07", 7);
            price.Add("DYCP08", 25);
            price.Add("DYCP09", 29);
            price.Add("DYCP10", 1.8);
            price.Add("DYCP11", 53);
            price.Add("DYCP12", 8.3);
            price.Add("DYCP13", 5.5);
            price.Add("DYCP14", 259);
            price.Add("DYCP15", 259);
            price.Add("DYCP16", 0);
            price.Add("DYCP17", 0);
            price.Add("DYCP18", 0);
            price.Add("DYCP19", 0);
            price.Add("DYCP20", 0);
            price.Add("DYCP21", 0);

            //创建数据表
            DataTable tblDatas = new DataTable();
            DataColumn dc = null;
            //单据类型
            dc = tblDatas.Columns.Add("店铺名称", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("收件人", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("省", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("市", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("区", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("手机", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("固话", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("邮编", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("网名", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("地址", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("发货方式", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("应收合计", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("邮费", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("优惠金额", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("商家编码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("货品数量", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("货品价格", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("备注", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("买家备注", Type.GetType("System.String"));

            DataRow newRow;

            int currentinmdex = 0;
            foreach (DataRow item in DT.Rows)
            {
                this.bgw_Order.ReportProgress(1, "--------------------------------------------------------\r\n");
                currentinmdex++;
                //5个品类

                for (int t = 7; t < DT.Columns.Count; t++)
                {
                    string temp_ProductCount = item[DT.Columns[t].ColumnName].ToString().Trim();
                    if (string.IsNullOrEmpty(temp_ProductCount) || Convert.ToInt32(temp_ProductCount) == 0)
                    {
                        continue;
                    }
                    newRow = tblDatas.NewRow();
                    newRow["单据类型"] = "05";
                    newRow["期初标志"] = "0";
                    newRow["发货单号"] = "DRFH" + DateTime.Now.ToString("yyyyMMddHHmm") + currentinmdex.ToString().PadLeft(4, '0');
                    newRow["发货日期"] = DateTime.Now.ToShortDateString();
                    newRow["业务类型"] = "普通销售";
                    newRow["销售类型编号"] = "01";//普通销售
                    newRow["客户编码"] = "07019";//环球中国
                    newRow["部门编码"] = "0201";//项目部
                    newRow["币种"] = "人民币";
                    newRow["汇率"] = "1";
                    newRow["表头税率"] = "17";
                    newRow["退货标志"] = "0";
                    newRow["先发货先开票标志"] = "0";
                    newRow["仓库编码"] = "01";
                    newRow["存货编码"] = DT.Columns[t].ColumnName;
                    newRow["主计量单位编码"] = "PCS";
                    newRow["主计量数量"] = temp_ProductCount;
                    newRow["报价含税"] = 1;
                    newRow["表头自定义项4"] = item["发货日期"].ToString();
                    newRow["表头自定义项11"] = item["批次单号"].ToString();
                    newRow["表头自定义项12"] = item["收货人姓名"].ToString();
                    newRow["表头自定义项13"] = item["地址"].ToString();
                    newRow["表头自定义项14"] = item["收货人电话"].ToString();
                    newRow["制单人"] = "范莲莲";

                    //含税单价_原币	无税单价	金额_原币_无税	税额_原币	价税合计_原币
                    //117 100 100 17  117

                    newRow["表头备注"] = "";
                    //newRow["含税单价_原币"] = price[DT.Columns[t].ColumnName] * (1.17);
                    //newRow["价税合计_原币"] = price[DT.Columns[t].ColumnName] * (1.17) * Convert.ToInt32(temp_ProductCount);
                    //newRow["税额_原币"] = price[DT.Columns[t].ColumnName] * 0.17;
                    //newRow["金额_原币_无税"] = price[DT.Columns[t].ColumnName] * Convert.ToInt32(temp_ProductCount);
                    //newRow["无税单价"] = price[DT.Columns[t].ColumnName];

                    double wushuidanjia = Math.Round(price[DT.Columns[t].ColumnName] / 1.17, 2, MidpointRounding.AwayFromZero);
                    newRow["含税单价_原币"] = price[DT.Columns[t].ColumnName];
                    newRow["价税合计_原币"] = price[DT.Columns[t].ColumnName] * Convert.ToInt32(temp_ProductCount);
                    newRow["税额_原币"] = price[DT.Columns[t].ColumnName] - wushuidanjia;
                    newRow["金额_原币_无税"] = wushuidanjia * Convert.ToInt32(temp_ProductCount);
                    newRow["无税单价"] = wushuidanjia;





                    tblDatas.Rows.Add(newRow);
                    this.bgw_Order.ReportProgress(1, currentinmdex.ToString().PadLeft(4, '0') + "\t" + DT.Columns[t].ColumnName + "\t" + item["收货人电话"].ToString() + "\t" + item["收货人姓名"].ToString() + "\t" + item["地址"].ToString() + "\r\n");
                }

                //this.bgw_Order.ReportProgress(1, "--------------------------------------------------------\r\n");
            }
            string[] arrayA = { "单据类型", "期初标志", "发货单号", "发货日期", "业务类型", "销售类型编号", "客户编码", "部门编码", "币种", "汇率", "表头税率",
                "退货标志", "先发货先开票标志", "仓库编码", "存货编码", "主计量单位编码", "主计量数量", "报价含税","无税单价" ,"金额_原币_无税","税额_原币","含税单价_原币","价税合计_原币",
                "表头备注", "表头自定义项4","表头自定义项11", "制单人", "表头自定义项12", "表头自定义项13", "表头自定义项14"};
            string[] arrayB = arrayA;
            NOPIHelper.Export(tblDatas, "", "E:\\其他\\123.xls", "sheet1", arrayA, arrayB);
        }
        //京东众筹
        private void ProcesOrder_JDZhongChou()
        {
            if (string.IsNullOrEmpty(GLOBAL_SAVEFILEPATH) || string.IsNullOrEmpty(GLOBAL_PATH))
            {
                this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>请选择要处理的文件，或者处理后的保存路径，");
                return;
            }

            if (string.IsNullOrEmpty(this.GLOBAL_SHOP_NAME))
            {
                this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>请增加店铺名称，");
                return;
            }
            //解析列明
            List<string> local_col_list = new List<string>(this.global_col_arry.Split(','));
            if (local_col_list == null || local_col_list.Count == 0)
            {
                this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>不存在列信息，");
                return;
            }
            this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>读取，");
            DataTable DT = null;
            try
            {
                DT = NOPIHelper.RenderDataTableFromExcel(this.GLOBAL_PATH, "Sheet1", 0);
            }
            catch (Exception ex)
            {
                this.bgw_Order.ReportProgress(1, ex.Message);
                return;
            }
            this.bgw_Order.ReportProgress(1, "完成<<<<<<<<<<<<\r\n");
            //判断文件是否有数据
            if (DT.Columns.Count == 0)
            {
                this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>excel无数据\r\n");
                return;
            }
            //重复列的数量
            int col_count = 0;
            foreach (DataColumn item in DT.Columns)
            {
                foreach (string item_col_name in local_col_list)
                {
                    if (item.ColumnName.Trim().Equals(item_col_name.Trim()))
                    {
                        col_count++;
                        break;
                    }
                }
            }
            if (col_count != local_col_list.Count)
            {
                this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>列不匹配，或多或少\r\n");
                return;
            }
            this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>创建数据表\r\n");
            //创建数据表
            DataTable tblDatas = new DataTable();

            DataColumn dc = null;
            dc = tblDatas.Columns.Add("店铺", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("订单编号", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("买家会员", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("支付金额", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("商品名称", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("商品代码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("规格代码", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("规格名称", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("数量", Type.GetType("System.Int32"));
            dc = tblDatas.Columns.Add("价格", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("商品备注", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("运费", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("买家留言", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("收货人", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("联系电话", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("联系手机", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("收货地址", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("省", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("市", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("区", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("邮编", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("订单创建时间", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("订单付款时间", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("发货时间", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("物流单号", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("物流公司", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("卖家备注", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("发票种类", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("发票类型", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("发票抬头", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("纳税人识别号", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("开户行", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("账号", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("地址", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("电话", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("是否手机订单", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("是否货到付款", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("支付方式", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("支付交易号", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("真实姓名", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("仓库名称", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("预计发货时间", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("预计送达时间", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("订单类型", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("是否分销商订单", Type.GetType("System.String"));
            DataRow newRow;

            int currentinmdex = 0;
            string prefix_order_code = DateTime.Now.ToString("yyyyMMddHHmm");
            foreach (DataRow item in DT.Rows)
            {
                currentinmdex++;
                string order_code = prefix_order_code + currentinmdex.ToString().PadLeft(5, '0');
                this.bgw_Order.ReportProgress(1, order_code + "\t" + item["联系手机"].ToString() + "  /  " + item["联系电话"].ToString() + item["收货人"].ToString() + "\r\n");
                foreach (string item_col_name in local_col_list)
                {
                    try
                    {
                        //如果没有数量就跳过
                        if (string.IsNullOrEmpty(item[item_col_name].ToString()) || Convert.ToInt32(item[item_col_name].ToString()) == 0)
                        {
                            continue;
                        }
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    newRow = tblDatas.NewRow();

                    newRow["店铺"] = this.GLOBAL_SHOP_NAME;
                    newRow["订单编号"] = order_code;
                    newRow["买家会员"] = item["收货人"].ToString();
                    newRow["收货人"] = item["收货人"].ToString();
                    newRow["联系电话"] = item["联系电话"].ToString();
                    newRow["联系手机"] = item["联系手机"].ToString();
                    newRow["省"] = item["省"].ToString();
                    newRow["市"] = item["市"].ToString();
                    newRow["区"] = item["区"].ToString();
                    newRow["收货地址"] = item["收货地址"].ToString();
                    newRow["支付金额"] = "0";
                    newRow["订单创建时间"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                    newRow["商品代码"] = item_col_name;
                    newRow["数量"] = Convert.ToInt32(item[item_col_name].ToString());
                    newRow["价格"] = "0";
                    newRow["买家留言"] = item["院线"].ToString() + "   " + item["公司机构"].ToString();
                    newRow["卖家备注"] = item["卖家备注"].ToString();
                    tblDatas.Rows.Add(newRow);
                }
                //this.bgw_Order.ReportProgress(1, "--------------------------------------------------------\r\n");
            }
            string[] arrayA = { "店铺", "订单编号", "买家会员", "支付金额", "商品名称", "商品代码", "规格代码", "规格名称", "数量", "价格", "商品备注", "运费", "买家留言", "收货人", "联系电话", "联系手机", "收货地址", "省", "市", "区", "邮编", "订单创建时间", "订单付款时间", "发货时间", "物流单号", "物流公司", "卖家备注", "发票种类", "发票类型", "发票抬头", "纳税人识别号", "开户行", "账号", "地址", "电话", "是否手机订单", "是否货到付款", "支付方式", "支付交易号", "真实姓名", "仓库名称", "预计发货时间", "预计送达时间", "订单类型", "是否分销商订单" };
            string[] arrayB = arrayA;
            NOPIHelper.Export(tblDatas, "", GLOBAL_SAVEFILEPATH + "\\" + Path.GetFileNameWithoutExtension(this.GLOBAL_PATH) + "_处理.xls", "sheet1", arrayA, arrayB);
        }

        private void btn_UpdateWuLiu_Click(object sender, EventArgs e)
        {
            txt_ShowInfo.Clear();
            this.bgw_Order.RunWorkerAsync("btn_UpdateWuLiu");
        }

        private void UpdateWuLiuNumber()
        {
            this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>读取，");
            DataTable DT = NOPIHelper.RenderDataTableFromExcel(this.GLOBAL_PATH, "Table1", 0);
            this.bgw_Order.ReportProgress(1, "完成<<<<<<<<<<<<\r\n");
            //判断文件是否有数据
            if (DT.Columns.Count == 0)
            {
                this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>excel无数据\r\n");
                return;
            }
            //发货单物流
            List<string> wuliu_fahuo = new List<string>();
            //出库单物流
            List<string> wuliu_chuku = new List<string>();
            Dictionary<string, List<string>> danhao = new Dictionary<string, List<string>>();
            this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>处理出库单和物流单之间关系，");
            foreach (DataRow item in DT.Rows)
            {
                if (string.IsNullOrEmpty(item["单号"].ToString().Trim()))
                {
                    continue;
                }
                if (danhao.Keys.Contains(item["目的地"].ToString().Trim()))
                {
                    danhao[item["目的地"].ToString().Trim()].Add(item["单号"].ToString().Trim());
                    continue;
                }
                danhao.Add(item["目的地"].ToString().Trim(), new List<String>() { item["单号"].ToString().Trim() });
            }
            this.bgw_Order.ReportProgress(1, "完成<<<<<<<<<<<<\r\n");
            FileStream stream = new FileStream(new DirectoryInfo(this.GLOBAL_PATH).Parent.FullName + @"\" + new FileInfo(this.GLOBAL_PATH).Name + "_" + DateTime.Now.ToShortDateString() + ".txt", FileMode.Create, FileAccess.ReadWrite);
            StreamWriter writer = new StreamWriter(stream);
            int countindex = 0;
            foreach (string item in danhao.Keys)
            {
                countindex++;
                if (danhao[item].Count() > 13)
                {
                    this.bgw_Order.ReportProgress(1, ">>>>>> " + countindex.ToString().PadLeft(4, '0') + "  " + item + "  " + danhao[item].Count() + "  物流单号个数超过13\r\n");
                    writer.Write("-->>>>>> " + countindex.ToString().PadLeft(4, '0') + "  " + item + "  " + danhao[item].Count() + "  物流单号个数超过13\r\n");
                }
                this.bgw_Order.ReportProgress(1, ">>>>>> " + countindex.ToString().PadLeft(4, '0') + "  " + item + "  " + string.Join(",", danhao[item].ToArray()) + "\r\n");
                writer.Write("UPDATE rdrecord32 SET cDefine11 = '" + string.Join(",", danhao[item].ToArray()) + "' WHERE cCode = '" + item + "'  --" + countindex.ToString().PadLeft(4, '0') + "\r\n");
            }
            writer.Close();
            stream.Close();
        }

        private void btn_UpdateFaHuoDan_Click(object sender, EventArgs e)
        {
            txt_ShowInfo.Clear();
            this.bgw_Order.RunWorkerAsync("btn_UpdateFaHuoDan");
        }

        private void UpdateFaHuoDan()
        {
            this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>读取，");
            DataTable DT = NOPIHelper.RenderDataTableFromExcel(this.GLOBAL_PATH, "Table1", 0);
            this.bgw_Order.ReportProgress(1, "完成<<<<<<<<<<<<\r\n");
            //判断文件是否有数据
            if (DT.Columns.Count == 0)
            {
                this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>excel无数据\r\n");
                return;
            }
            //发货单物流
            List<string> wuliu_fahuo = new List<string>();
            //出库单物流
            List<string> wuliu_chuku = new List<string>();

            Dictionary<string, List<string>> danhao = new Dictionary<string, List<string>>();
            Dictionary<string, List<string>> fahuodan = new Dictionary<string, List<string>>();
            Dictionary<string, List<string>> fahuodan_wuliudan = new Dictionary<string, List<string>>();
            this.bgw_Order.ReportProgress(1, ">>>>>>>>>>>>处理出库单和物流单之间关系，");
            foreach (DataRow item in DT.Rows)
            {
                if (string.IsNullOrEmpty(item["物流单号"].ToString().Trim()))
                {
                    continue;
                }
                if (danhao.Keys.Contains(item["出库单号"].ToString().Trim()))
                {
                    danhao[item["出库单号"].ToString().Trim()].AddRange(new List<string>(item["物流单号"].ToString().Trim().Split(',')));
                    continue;
                }
                danhao.Add(item["出库单号"].ToString().Trim(), new List<string>(item["物流单号"].ToString().Trim().Split(',')));
            }

            foreach (DataRow item in DT.Rows)
            {
                if (string.IsNullOrEmpty(item["出库单号"].ToString().Trim()))
                {
                    continue;
                }
                if (fahuodan.Keys.Contains(item["业务号"].ToString().Trim()))
                {
                    fahuodan[item["业务号"].ToString().Trim()].Add(item["出库单号"].ToString().Trim());
                    continue;
                }
                fahuodan.Add(item["业务号"].ToString().Trim(), new List<String>() { item["出库单号"].ToString().Trim() });
            }

            foreach (string item in fahuodan.Keys)
            {
                List<string> wuliudanhao = new List<string>();
                foreach (string item_chukudanhao in fahuodan[item])
                {
                    foreach (string item_wuliudanhao in danhao[item_chukudanhao])
                    {
                        wuliudanhao.Add(item_wuliudanhao);
                    }
                }
                fahuodan_wuliudan.Add(item, wuliudanhao);
            }

            this.bgw_Order.ReportProgress(1, "完成<<<<<<<<<<<<\r\n");
            FileStream stream = new FileStream(new DirectoryInfo(this.GLOBAL_PATH).Parent.FullName + @"\" + new FileInfo(this.GLOBAL_PATH).Name + "_" + DateTime.Now.ToShortDateString() + ".txt", FileMode.Create, FileAccess.ReadWrite);
            StreamWriter writer = new StreamWriter(stream);
            int countindex = 0;
            foreach (string item in fahuodan_wuliudan.Keys)
            {
                countindex++;
                this.bgw_Order.ReportProgress(1, ">>>>>> " + countindex.ToString().PadLeft(4, '0') + "  " + item + "  " + string.Join(",", fahuodan_wuliudan[item].ToArray()) + "\r\n");
                if (fahuodan_wuliudan[item].Count() > 13)
                {
                    this.bgw_Order.ReportProgress(1, ">>>>>> " + countindex.ToString().PadLeft(4, '0') + "  " + item + "  " + fahuodan_wuliudan[item].Count() + "  物流单号个数超过13\r\n");
                    writer.Write("-->>>>>> " + countindex.ToString().PadLeft(4, '0') + "  " + item + "  " + fahuodan_wuliudan[item].Count() + "  物流单号个数超过13\r\n");
                }

                writer.Write("UPDATE DispatchList SET cDefine11 = '" + string.Join(",", fahuodan_wuliudan[item].ToArray()) + "' WHERE cDLCode = '" + item + "'  --" + countindex.ToString().PadLeft(4, '0') + "'\r\n");
            }
            writer.Write("");
            writer.Close();
            stream.Close();
        }

        private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
        {
        }

        private void btn_SavePath_Click(object sender, EventArgs e)
        {
            fbd_FilePath.Description = "请选择Txt所在文件夹";
            if (fbd_FilePath.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (string.IsNullOrEmpty(fbd_FilePath.SelectedPath))
                {
                    txt_ShowInfo.AppendText("文件夹路径不能为空");
                    return;
                }
                GLOBAL_SAVEFILEPATH = fbd_FilePath.SelectedPath;
                txt_save_path.AppendText(GLOBAL_SAVEFILEPATH);
            }
        }
    }
}
